package com.ruoyi.chat.controller;

import com.ruoyi.chat.service.ChatService;
import com.ruoyi.common.annotation.Anonymous;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

/**
 * 聊天控制器，提供基于LangChain4j的流式聊天API
 *
 * @author WaKy
 * @version 1.0
 */
@RestController
@Slf4j
@CrossOrigin(origins = "*")  // 允许所有来源的跨域请求
@RequestMapping("/api/chat")
@Anonymous  // 允许匿名访问
@Tag(name = "ChatController", description = "聊天API")
public class ChatController {

    private final ChatService chatService;

    public ChatController(ChatService chatService) {
        this.chatService = chatService;
    }

    /**
     * 聊天API
     *
     * @param question 用户问题
     * @return SseEmitter 用于流式响应的对象
     * 之前返回值是 SseEmitter
     * 现在改为 Flux<String>
     *     // DeepSeek官方API不支持此种方式，所以我们使用其他模型(如阿里百炼DeepSeek)
     */
    @Operation(
            summary = "聊天API",
            description = "聊天API",
            parameters = {
                    @Parameter(name = "question", description = "用户问题"),
                    @Parameter(name = "memoryId", description = "会话ID")
            }
    )
    @GetMapping(value = "/stream", produces = "text/stream;charset=UTF-8")
    public Flux<String> streamChat(@RequestParam(value = "question", defaultValue = "你好") String question,
                                   @RequestParam(value = "memoryId") Long memoryId) {
        log.info("接收到流式聊天请求，问题：{}", question);
        return chatService.streamChat(question, memoryId);
    }

}
